# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import (
        QWidget, QDialog, QApplication,
        QLabel, QPushButton, QLineEdit, QComboBox,
        QCheckBox, QSpinBox,
        QHBoxLayout, QVBoxLayout,
        QSpacerItem, QSizePolicy,
        QDoubleSpinBox, QFileDialog,
        QTableWidgetItem, QTableWidget, QMessageBox
        )
from PyQt5.QtGui import (
        QPainter, QPen, QImage, QPixmap, QBrush, QColor
        )
from PyQt5.QtCore import (
        QCoreApplication, QRect, Qt, QObject, pyqtSignal, QTimer
        )
import sys, os, re, time, json, requests, argparse

import logging
log_format = '%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)s] - %(message)s'
logging.basicConfig(level = logging.INFO, format = log_format)
logger = logging.getLogger('correct.log')

class Canvas(QLabel):
    def __init__(self):
        super().__init__()

        self.lineColor = QColor('gray')
        self.fillColor = QColor('blue')
        self.step = 15
        self.row = 15
        self.col = 4
        self.W = self.col * self.step + 1
        self.H = self.row * self.step + 1
        self.canvas = QPixmap(self.W, self.H)
        self.canvas.fill(QColor('white'))
        self.setPixmap(self.canvas)
        self.drawGrid(self.row, self.col)

    def drawGrid(self, row, col):
        painter = QPainter(self.pixmap())
        pen = painter.pen()
        pen.setWidth(1)
        pen.setColor(self.lineColor)
        painter.setPen(pen)
        for r in range(row + 1):
            painter.drawLine(0, r * self.step, self.W, r * self.step)
        for c in range(col + 1):
            painter.drawLine(c * self.step, 0, c * self.step, self.H)
        painter.end()
        self.update()

    def mousePressEvent(self, event):
        pass

    def mouseMoveEvent(self, event):
        return

        painter = QPainter(self.pixmap())
        pen = painter.pen()
        pen.setWidth(4)
        pen.setColor(self.fillColor)
        painter.setPen(pen)
        # painter.drawLine(self.last_x, self.last_y, e.x(), e.y())
        painter.end()
        self.update()

    def mouseReleaseEvent(self, event):
        pass

class ScanCase(QWidget):
    def __init__(self):
        super().__init__()



class FocusCollect(QDialog):
    def __init__(self):
        super(FocusCollect, self).__init__()
        self.initUI()
        self.setSlots()

        self.updateTimer = QTimer(self)
        self.updateTimer.timeout.connect(self.updateStatus)
        self.updateTimer.start(2000)

    def initUI(self):
        self.setWindowTitle("对焦数据采集")
        self.setObjectName("FocusCollect")
        # self.resize(800, 600)
        lytMain = QVBoxLayout(self)

        lytLine = QHBoxLayout()
        self.chkHst = QCheckBox('高通量')
        lytLine.addWidget(self.chkHst)
        lytMain.addLayout(lytLine)

        lytLine = QHBoxLayout()
        self.lst_canvas = []
        for i in range(3):
            canvas = Canvas()
            lytLine.addWidget(canvas)
            self.lst_canvas.append(canvas)
        lytMain.addLayout(lytLine)

        lytLine = QHBoxLayout()
        lytLine.addWidget(QLabel('玻片箱'))
        self.spBoxBgn = QSpinBox()
        self.spBoxBgn.setRange(1, 3)
        self.spBoxBgn.setValue(1)
        self.spBoxBgn.setSingleStep(1)
        lytLine.addWidget(self.spBoxBgn)
        lytLine.addWidget(QLabel('~'))
        self.spBoxEnd = QSpinBox()
        self.spBoxEnd.setRange(1, 3)
        self.spBoxEnd.setValue(3)
        self.spBoxEnd.setSingleStep(1)
        lytLine.addWidget(self.spBoxEnd)

        lytLine.addWidget(QLabel('玻片架'))
        self.spRackBgn = QSpinBox()
        self.spRackBgn.setRange(1, 15)
        self.spRackBgn.setValue(1)
        self.spRackBgn.setSingleStep(1)
        lytLine.addWidget(self.spRackBgn)
        lytLine.addWidget(QLabel('~'))
        self.spRackEnd = QSpinBox()
        self.spRackEnd.setRange(1, 15)
        self.spRackEnd.setValue(15)
        self.spRackEnd.setSingleStep(1)
        lytLine.addWidget(self.spRackEnd)
        lytLine.addStretch()
        lytMain.addLayout(lytLine)

        lytLine = QHBoxLayout()
        lytLine.addWidget(QLabel('间隔'))
        self.txtStep = QLineEdit('1')
        self.txtStep.setFixedWidth(45)
        lytLine.addWidget(self.txtStep)
        lytLine.addWidget(QLabel('um对焦'))
        lytLine.addStretch()
        lytLine.addWidget(QLabel('玻片'))
        self.lst_chkGls = []
        for i in range(4):
            chkGls = QCheckBox(f'{i}')
            chkGls.idx = i
            self.lst_chkGls.append(chkGls)
            lytLine.addWidget(chkGls)
        lytMain.addLayout(lytLine)

        lytMain.addStretch()

        lytLine = QHBoxLayout()
        self.chkSaveRaw = QCheckBox('保存原始对焦数据')
        lytLine.addWidget(self.chkSaveRaw)
        self.btnRun = QPushButton('开始')
        self.btnRun.setFixedWidth(80)
        lytLine.addWidget(self.btnRun)
        lytMain.addLayout(lytLine)

    def setSlots(self):
        self.chkHst.stateChanged.connect(self.setEnableHst)
        pass

    def setEnableHst(self):
        isEnabled = self.chkHst.isChecked()
        self.spBoxBgn.setEnable(isEnabled)
        self.spBoxEnd.setEnable(isEnabled)
        self.spRackBgn.setEnable(isEnabled)
        self.spRackEnd.setEnable(isEnabled)

    def updateStatus(self):
        pass

    def closeEvent(self, event):
        self.updateTimer.stop()
        self.updateTimer.timeout.disconnect(self.updateStatus)
        event.accept()

if __name__=='__main__':
    parser = argparse.ArgumentParser()
    # parser.add_argument('-m', '--mode', default='cli', choices=['cli', 'gui'])
    args = parser.parse_args()

    QCoreApplication(sys.argv)
    QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)

    app = QApplication(sys.argv)
    win = FocusCollect()
    win.show()
    sys.exit(app.exec())

